package com.wxd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.reggie.common.CustomException;
import com.wxd.reggie.entity.Category;
import com.wxd.reggie.entity.Dish;
import com.wxd.reggie.entity.Setmeal;
import com.wxd.reggie.mapper.CategoryMapper;
import com.wxd.reggie.service.CategoryService;
import com.wxd.reggie.service.DishService;
import com.wxd.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void delCategory(Long id) {
        // 1.根据id查询该分类下是否有菜品 若有 则抛业务异常
        LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper<>();
        dishLqw.eq(Dish::getCategoryId, id);
        int count = dishService.count(dishLqw);
        if (count > 0) {
            // 有菜品
            throw new CustomException("该分类下有菜品，不可删除");
        }
        // 2.根据id查询该分类下是否有套餐 若有 则抛业务异常
        LambdaQueryWrapper<Setmeal> setmealLqw = new LambdaQueryWrapper<>();
        setmealLqw.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(setmealLqw);
        if (count1 > 0) {
            // 有套餐
            throw new CustomException("该分类下有套餐，不可删除");
        }
        // 3. 分类下无具体菜品或套餐 则正常进行删除
        super.removeById(id);
    }
}
